package com.spsoft.vatoutinv.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.controller.BaseController;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.model.Result;
import com.spsoft.core.model.bo.TokenUser;
import com.spsoft.vatoutinv.model.dto.vmsavalibletaxrate.VmsAvalibleTaxRateDto;
import com.spsoft.vatoutinv.model.dto.vmsavalibletaxrate.VmsAvalibleTaxRateLogBaseDto;
import com.spsoft.vatoutinv.model.vo.vmsavalibletaxrate.VmsAvalibleTaxRateLogBaseVo;
import com.spsoft.vatoutinv.model.vo.vmsavalibletaxrate.VmsAvalibleTaxRateVo;
import com.spsoft.vatoutinv.service.vmsavalibletaxrate.VmsAvalibleTaxRateLogBaseService;
import com.spsoft.vatoutinv.service.vmsavalibletaxrate.VmsAvalibleTaxRateLogService;
import com.spsoft.vatoutinv.service.vmsavalibletaxrate.VmsAvalibleTaxRateService;
import com.spsoft.vatoutinv.service.vmsenterpriseinfo.VmsEnterpriseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 可用税率信息查询记录列表表 前端控制器
 * </p>
 *
 * @author chenxudong
 * @since 2023-11-23
 */
@RestController
@Api(tags = {"可用税率信息管理"})
@RequestMapping("/vmsAvalibleTaxRate")
@RequiredArgsConstructor
@Validated
@Slf4j
public class VmsAvalibleTaxRateController extends BaseController {

    public static final String TAX_RATE_MSG = "(可用税率信息控制层)";

    public static final String TAX_RATE_LOG_MSG = "(可用税率信息获取记录控制层)";

    private final VmsEnterpriseInfoService vmsEnterpriseInfoService;

    private final VmsAvalibleTaxRateService vmsAvalibleTaxRateService;

    private final VmsAvalibleTaxRateLogService vmsAvalibleTaxRateLogService;

    private final VmsAvalibleTaxRateLogBaseService vmsAvalibleTaxRateLogBaseService;

    @ApiOperation("查询可用税率信息")
    @AnonymousAccess(value = "/queryVmsAvalibleTaxRate", method = RequestMethod.POST, permissionCode = "VMS_AVAILABLETAX")
    @Log(value = "查询可用税率信息接口", type = LogActionType.SELECT, menuName = LogMenuEnum.TAX_NUMBER_INFO)
    public Result<IPage<VmsAvalibleTaxRateVo>> queryVmsAvalibleTaxRate(@RequestBody VmsAvalibleTaxRateDto vmsAvalibleTaxRateDto) {
        vmsAvalibleTaxRateDto.setDeptCode(getDeptCode());
        log.info("{}查询可用税率信息入参：{}", TAX_RATE_MSG, JSONObject.toJSONString(vmsAvalibleTaxRateDto));
        return vmsAvalibleTaxRateService.queryVmsAvalibleTaxRate(vmsAvalibleTaxRateDto);
    }

    @ApiOperation("调用乐企获取可用税率信息")
    @AnonymousAccess(value = "/getVmsAvalibleTaxRateByLq", method = RequestMethod.POST, permissionCode = "VMS_AVALIBLE_TAX_RATE_ADD")
    @Log(value = "调用乐企获取可用税率信息", type = LogActionType.ADD, menuName = LogMenuEnum.TAX_NUMBER_INFO)
    public Result<Object> getVmsAvalibleTaxRateByLq() {
        TokenUser userInfo = getUser();
        VmsAvalibleTaxRateDto vmsAvalibleTaxRateDto = new VmsAvalibleTaxRateDto();
        vmsAvalibleTaxRateDto.setDeptCode(userInfo.getDeptCode());
        vmsAvalibleTaxRateDto.setNsrsbh(vmsEnterpriseInfoService.getNsrsbh());
        log.info("{}调用乐企获取可用税率信息入参：{}", TAX_RATE_MSG, JSONObject.toJSONString(vmsAvalibleTaxRateDto));
        return vmsAvalibleTaxRateService.getVmsAvalibleTaxRateByLq(vmsAvalibleTaxRateDto, userInfo);
    }

    @ApiOperation("查询可用税率信息获取记录")
    @AnonymousAccess(value = "/queryVmsAvalibleTaxRateLog", method = RequestMethod.POST, permissionCode = "VMS_AVAILABLETAX")
    @Log(value = "查询可用税率信息获取记录接口", type = LogActionType.SELECT, menuName = LogMenuEnum.TAX_NUMBER_INFO)
    public Result<IPage<VmsAvalibleTaxRateLogBaseVo>> queryVmsAvalibleTaxRateLog(@RequestBody VmsAvalibleTaxRateLogBaseDto vmsAvalibleTaxRateLogBaseDto) {
        vmsAvalibleTaxRateLogBaseDto.setDeptCode(getDeptCode());
        log.info("{}查询可用税率信息获取记录入参：{}", TAX_RATE_LOG_MSG, JSONObject.toJSONString(vmsAvalibleTaxRateLogBaseDto));
        return vmsAvalibleTaxRateLogBaseService.queryVmsAvalibleTaxRateLog(vmsAvalibleTaxRateLogBaseDto);
    }

    @ApiOperation("查询可用税率信息获取记录详情")
    @AnonymousAccess(value = "/queryVmsAvalibleTaxRateLogDetail", method = RequestMethod.POST, permissionCode = "VMS_AVAILABLETAX")
    @Log(value = "查询可用税率信息获取记录详情接口", type = LogActionType.SELECT, menuName = LogMenuEnum.TAX_NUMBER_INFO)
    public Result<IPage<VmsAvalibleTaxRateVo>> queryVmsAvalibleTaxRateLogDetail(@RequestBody VmsAvalibleTaxRateLogBaseDto vmsAvalibleTaxRateLogBaseDto) {
        log.info("{}查询可用税率信息获取记录详情入参：{}", TAX_RATE_LOG_MSG, JSONObject.toJSONString(vmsAvalibleTaxRateLogBaseDto));
        if (StringUtils.isBlank(vmsAvalibleTaxRateLogBaseDto.getLogBaseId())) {
            return Result.ERROR(ResponseEnum.PARAM_VALIDATED_ERROR);
        }
        return vmsAvalibleTaxRateLogService.queryVmsAvalibleTaxRateLogDetail(vmsAvalibleTaxRateLogBaseDto);
    }

    @ApiOperation("导出可用税率信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "csxz", value = "参数性质;1：税率，2：征收率", dataTypeClass = String.class, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "cszt", value = "参数状态;0：启用 1：停用", dataTypeClass = String.class, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "slzsl", value = "税率/征收率", dataTypeClass = String.class, dataType = "String", paramType = "query")
    })
    @AnonymousAccess(value = "/exportVmsAvalibleTaxRate", method = RequestMethod.POST, permissionCode = "VMS_ABALIBLE_TAX_RATE_EXPORT")
    @Log(value = "导出可用税率信息接口", type = LogActionType.SELECT, menuName = LogMenuEnum.TAX_NUMBER_INFO)
    public void exportVmsAvalibleTaxRate(@RequestParam(required = false) String csxz, @RequestParam(required = false) String cszt,
                                         @RequestParam(required = false) String slzsl, HttpServletResponse response) {
        VmsAvalibleTaxRateDto vmsAvalibleTaxRateDto = new VmsAvalibleTaxRateDto();
        vmsAvalibleTaxRateDto.setCsxz(csxz);
        vmsAvalibleTaxRateDto.setCszt(cszt);
        vmsAvalibleTaxRateDto.setSlzsl(slzsl);
        vmsAvalibleTaxRateDto.setDeptCode(getDeptCode());
        log.info("{}导出可用税率信息入参：{}", TAX_RATE_MSG, JSONObject.toJSONString(vmsAvalibleTaxRateDto));
        vmsAvalibleTaxRateService.exportVmsAvalibleTaxRate(vmsAvalibleTaxRateDto, response);
    }

    @ApiOperation("导出可用税率历史明细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "logBaseId", value = "日志ID", dataTypeClass = Long.class, dataType = "long", paramType = "query", required = true)
    })
    @AnonymousAccess(value = "/exportVmsAvalibleTaxRateLogDetail", method = RequestMethod.POST, permissionCode = "VMS_ABALIBLE_TAX_RATE_EXPORT")
    @Log(value = "导出可用税率历史明细信息接口", type = LogActionType.SELECT, menuName = LogMenuEnum.TAX_NUMBER_INFO)
    public void exportVmsAvalibleTaxRateLogDetail(@RequestParam(value = "logBaseId") Long logBaseId, HttpServletResponse response) {
        log.info("{}导出可用税率历史明细入参logBaseId：{}", TAX_RATE_LOG_MSG, logBaseId);
        vmsAvalibleTaxRateLogService.exportVmsAvalibleTaxRateLogDetail(logBaseId, response);
    }

}

